/*
 * @Author: xiaosihan
 * @Date: 2024-11-07 21:19:08
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-11-07 21:59:24
 */

import { AmbientLight, DirectionalLight, PointLight, Vector3 } from "three";

import ThreeBase from "three-base";
import FPScontrol from "three-base/FPScontrol";
import Ground1 from "three-base/Ground1";
import MeshBase from "three-base/MeshBase";
import Sky from "three-base/Sky";

/**
 * @class FpsDemoRender
 * @extends {ThreeBase}
 */
class FpsDemoRender extends ThreeBase {
  constructor() {
    super();
  }

  // 环境光
  ambientLight = (() => {
    const ambientLight = new AmbientLight("#fff", 3);
    this.scene.add(ambientLight);
    return ambientLight;
  })();

  //点光源
  pointLight = (() => {
    const pointLight = new PointLight("#fff", 1.5);
    pointLight.position.set(50, 40, 50);
    this.scene.add(pointLight);
    return pointLight;
  })();

  // 方向光
  directionalLight = (() => {
    const directionalLight = new DirectionalLight("#fff", 1.5);
    directionalLight.position.set(-10, 10, 5);
    directionalLight.target.position.set(0, 0, 0);
    directionalLight.castShadow = true;
    directionalLight.shadow.mapSize.set(1024, 1024);
    this.scene.add(directionalLight);
    return directionalLight;
  })();

  init() {
    super.init();
    // this.axesHelper.visible = false;
    this.gridHelper.visible = false;
    this.controls.enabled = false;
    // this.controls.enableZoom = false;
    // this.controls.enablePan = false;

    this.resetLookAt({
      center: new Vector3(0, 2, 0),
      position: new Vector3(10, 2, 10),
    });
  }

  //第一人称控制器
  fpsControl = (() => {
    const fpsControl = new FPScontrol(
      this.perspectiveCamera,
      this.renderer.domElement
    );
    this.perspectiveCamera.rotation.z = 0;
    return fpsControl;
  })();

  // 天空
  sky = (() => {
    const sky = new Sky();
    this.add(sky);
    return sky;
  })();

  //装饰地板
  floor = (() => {
    const floor = new Ground1();
    floor.setSize(100, 100);
    this.fpsControl.addOctree(floor);
    this.add(floor);
    return floor;
  })();

  //场景底板
  floorMesh = (() => {
    const floorMesh = new MeshBase("./model/floor.glb");
    floorMesh.position.set(0, 1.5, 0);
    floorMesh.addEventListener("loaded", () => {
      this.fpsControl.addOctree(floorMesh);
    });
    this.add(floorMesh);
    return floorMesh;
  })();

  render() {
    this.fpsControl.update();

    super.render();
  }
}

const fpsDemoRender = (window.fpsDemoRender = new FpsDemoRender());

export default fpsDemoRender;
